package com.topway.dsmflex.services.synchro;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.Consts;
import com.topway.dsmflex.dao.synchro.TableDao;
import com.topway.dsmflex.model.synchro.DataColumnBean;
import com.topway.dsmflex.model.synchro.DataSourceBean;
import com.topway.dsmflex.model.synchro.DataTableBean;
import com.topway.dsmflex.services.synchro.base.AbstractBaseServices;
import com.topway.dsmflex.util.ConvertUtil;



/**
 * 
 * @author liusy 2013-05-2
 *
 */
public  class TableServices extends AbstractBaseServices<DataTableBean>{

	private static Logger log = Logger.getLogger(TableDao.class);
	
	private static TableServices tableServices = new TableServices(Consts.DATAFILE_TABLE);
	
	protected TableDao tableDao = null;

	@SuppressWarnings("unchecked")
	public TableServices(String dataFilePath)
	{
		super(dataFilePath);
		this.tableDao = TableDao.getTableDao();
	}

	public synchronized static TableServices getTableServices()
	{
		return tableServices;
	}
	
	public List<DataColumnBean> getColumnInfo(DataTableBean dt)
	{
	 	List<DataColumnBean> dcListT = new ArrayList<DataColumnBean>();
	 	try {
	 	dcListT = (List<DataColumnBean>) tableDao.getColumnInfo(dt.getSourceDataSource(), dt.getTableNameSource());
	 	this.setSourceColumn(tableDao.getSourceColumn());
	 	if(mapTable.containsKey( dt.getTableNameSource()))
	 	{
	 		DataTableBean _dt = mapTable.get( dt.getTableNameSource());
	 		List<DataColumnBean> dcListS = _dt.getDataColumnBean();
	 		if(null==dcListS || dcListS.size()==0)
	 			return dcListT;
	 		DataColumnBean dcT;
	 		DataColumnBean dcS;
			for (int j = 0; j < dcListT.size(); j++) {
				dcT = dcListT.get(j);
		 		for (int i = 0; i < dcListS.size(); i++) {
					dcS = dcListS.get(i);
					if(dcT.getColumnNameSource().equals(dcS.getColumnNameSource()))
						try {
							ConvertUtil.convertToPool(dcT, dcS);
						} catch (Exception e) {
							e.printStackTrace();
						}
				}
			}
	 	}
		} catch (Exception e) {
			e.printStackTrace();
		}
	 	return dcListT;
    }
	
	public List<DataTableBean> getAllDataTable(DataSourceBean  dsSource) {
		List<DataTableBean> dtList = tableDao.getAllDataTable(dsSource);
		String [] _sourceTables = new String [dtList.size()];
		DataTableBean dt;
		for (int i = 0; i < dtList.size(); i++) {
			dt = dtList.get(i);
			_sourceTables[i] = dt.getTableNameSource();
			if(mapTable.containsKey(dt.getTableNameSource()))
			{
				dtList.remove(i);
				dtList.add(i,  mapTable.get(dt.getTableNameSource()));
			}
		}
		this.setSourceTable(_sourceTables);
		return dtList;
	}
	
	public String [] getTargetTablesByDb(DataTableBean dt)
	{
		setTargetTable(this.tableDao.getAllDataTableArray(dt.getTargetDataSource()));
		return this.targetTable;
	}
	
	public String [] getColumnInfoArray(DataTableBean dt)
	{
	 	this.setTargetColumn(tableDao.getColumnInfoArray(dt.getTargetDataSource(), dt.getTableNameTarget()));
	 	return this.targetColumn;
    }
	
	
	   public static void main(String[] args) {
		String str = "a,";
			String [] a = str.split(",");
			System.out.println(a.length);
	}
     
}
